﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.Page
{
    public partial class quan_ly_quyen : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtUserPermisstion;
        private DataTable dtUserPermisstionDetail_Edit;
        private DataTable dtRoles;
        private DataTable dtFunctionPermissModun;
        private DataTable dtFillterRoles;
        private DataTable dtFunctionPermiss;
        private DataTable dtFunctionPermissFillter;
        private DataTable dtCheckInsert;
        private DataTable dtDelUserPermiss;
        private DataTable dtAutoComplete;
        private DataTable dtFillterModun;
        protected string StrAlert = "";
        protected string SuggestionList;
        private DataTable dtSearch;
        clsBusiness_tb_UserPermiss clsUserPermiss = new clsBusiness_tb_UserPermiss();
        clsBusiness_tb_Roles clsRoles = new clsBusiness_tb_Roles();
        clsBusiness_tb_PermissFunc clsFunctionPermiss = new clsBusiness_tb_PermissFunc();
        private static bool flag = false;
        #endregion

        #region[Controler]
        protected void Page_Load(object sender, EventArgs e)
        {
            Autocomplate();
            CheckPermisstion();
            if (!IsPostBack)
            {
                BindGridUserPermisstion();
                BindGridRoles();
                BindGridFuncPerrmiss();
            }
        }
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            flag = true;
            lbID.Text = "";
            ClearCheckBox();
            PanelChucNang.Visible = true;
            PanelModun.Visible = false;
            PanelSelectModun.Visible = true;
            drRolesGroup.Enabled = true;
            OpenDialog("#quanlyquyen");
            imgSave.ToolTip = "Thêm mới";
            upNewUpdatePanel.Update();
            imgSave.Visible = true;
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            lbThongBaoLoi.Text = "";
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkView != null)
                {
                    if (chkView.Checked)
                    {
                        PanelChucNang.Visible = false;
                        PanelModun.Visible = true;
                        drRolesGroup.Enabled = false;
                        PanelSelectModun.Visible = false;
                        BindGridUserPermisstionDetail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        OpenDialog("#quanlyquyen");
                        upNewUpdatePanel.Update();
                        flag = false;
                        imgSave.ToolTip = "Cập nhật";
                    }
                }
            }
        }
        protected void cmdDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                if (chkHide != null)
                {
                    if (chkHide.Checked)
                    {
                        BindGridUserPermisstionDetail_Edit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
                        lbThongBaoXoa.Text = "Bạn có muốn xoá mẫu tin này không !";
                        OpenDialog("#thongbaoxoa");
                        upDatePanelThongBaoXoa.Update();
                        cmdAgree.Visible = true;
                        cmdClose.Text = "Bỏ qua";
                    }
                }
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            drMoDun.SelectedIndex = -1;
            drNhomQuyen.SelectedIndex = -1;
            BindGridRoles();
        }
        //----------------List-View---------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyPhanQuyen, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                lbThongBaoLoi.Text = "";
                if (e.CommandName == "Accept")
                {
                    if (Edit == false)
                    {
                        imgSave.Visible = false;
                        PanelChucNang.Visible = false;
                        PanelModun.Visible = true;
                        drRolesGroup.Enabled = false;
                        PanelSelectModun.Visible = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridUserPermisstionDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#quanlyquyen");
                        upNewUpdatePanel.Update();
                        flag = false;
                        imgSave.ToolTip = "Cập nhật";
                    }
                    else
                    {
                        imgSave.Visible = true;
                        PanelChucNang.Visible = false;
                        PanelModun.Visible = true;
                        drRolesGroup.Enabled = false;
                        PanelSelectModun.Visible = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridUserPermisstionDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#quanlyquyen");
                        upNewUpdatePanel.Update();
                        flag = false;
                        imgSave.ToolTip = "Cập nhật";
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");


                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";
            }
        }
        //--------------DataPager-----------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 17;
            BindGridUserPermisstion();
            if (drNhomQuyen.SelectedValue.ToString() != "-1")
            {
                FillterRoles(int.Parse(drNhomQuyen.SelectedValue.ToString()));
            }
        }
        //--------------Button-Popup--------------------
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (drRolesGroup.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn nhóm quyền !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }

            if (drRolesGroup.SelectedValue.ToString() != "-1")
            {
                Insert();
            }
        }
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {
            try
            {
                VanTaiBLL.clsBusiness_tb_UserPermiss.DeleteRoles(int.Parse(lbIDXoa.Text));
                lbThongBaoXoa.Text = "Xoá thành công mẫu tin !";
                OpenDialog("#thongbaoxoa");
                upDatePanelThongBaoXoa.Update();
                cmdAgree.Visible = false;
                cmdClose.Text = "Đóng lại";
            }
            catch (Exception ex)
            {
                lbThongBaoXoa.Text = ex.ToString();
                OpenDialog("#thongbaoxoa");
                upDatePanelThongBaoXoa.Update();
                cmdAgree.Visible = false;
                cmdClose.Text = "Đóng lại";
            }
        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        //-------------dropdownlist---------------------
        protected void drNhomQuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drNhomQuyen.SelectedValue.ToString() != "-1")
            {
                FillterRoles(int.Parse(drNhomQuyen.SelectedValue.ToString()));
            }
            else
            {
                BindGridUserPermisstion();
            }
        }
        protected void drRolesGroup_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void drChucNangEdit_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void drSelectModun_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drSelectModun.SelectedValue.ToString() != "-1")
            {
                BindGridPermisstionModun(drSelectModun.SelectedValue.ToString());
            }
            else
            {
                BindGridFuncPerrmiss();
            }
        }
        protected void drMoDun_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drMoDun.SelectedValue.ToString() != "-1")
            {
                FillterModun(int.Parse(drMoDun.SelectedValue.ToString()));
            }
            else
            {
                BindGridUserPermisstion();
            }
        }
        //-------------Checkbox---------------
        protected void chkCheckAllModun_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem listitem in chkChucNangList.Items)
            {
                listitem.Selected = chkCheckAllModun.Checked;
            }
        }
        #endregion

        #region[Function]
        protected void BindGridUserPermisstion()
        {
            if (drNhomQuyen.SelectedValue.ToString() == "-1" && drMoDun.SelectedValue.ToString() == "-1")
            {
                dtFillterRoles = clsUserPermiss.BindGridUserPermissDetail_Edit(int.Parse(drNhomQuyen.SelectedValue.ToString()));
                dtFillterModun = clsUserPermiss.FillterModun(int.Parse(drMoDun.SelectedValue.ToString()));
                dtUserPermisstion = clsUserPermiss.Search(txtSearch.Text);
                if (dtUserPermisstion != null && dtUserPermisstion.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtUserPermisstion;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
                else
                {
                    ListViewAll.Visible = false;
                    DataPagerListAll.Visible = false;
                }
            }
            else
            {
               
                if (dtFillterRoles != null && dtFillterRoles.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtFillterRoles;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
                if (dtFillterModun != null && dtFillterModun.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtFillterModun;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;
                }
            }
        }
        protected void BindGridRoles()
        {
            dtRoles = clsRoles.BindGridRoles();
            dtFunctionPermissFillter = clsFunctionPermiss.BindGridPermissFunc();
            if (dtRoles != null && dtRoles.Rows.Count > 0)
            {
                drNhomQuyen.DataSource = dtRoles;
                drNhomQuyen.DataTextField = "NameRoles";
                drNhomQuyen.DataValueField = "RolesID";
                drNhomQuyen.DataBind();

                drRolesGroup.DataSource = dtRoles;
                drRolesGroup.DataTextField = "NameRoles";
                drRolesGroup.DataValueField = "RolesID";
                drRolesGroup.DataBind();
            }
            if (dtFunctionPermissFillter != null && dtFunctionPermissFillter.Rows.Count > 0)
            {
                drMoDun.DataSource = dtFunctionPermissFillter;
                drMoDun.DataTextField = "DescriptstionName";
                drMoDun.DataValueField = "PermissFuncID";
                drMoDun.DataBind();
            }
        }
        protected void BindGridFuncPerrmiss()
        {
            dtFunctionPermiss = clsFunctionPermiss.BindGridPermissFunc();
            dtFunctionPermissModun = clsFunctionPermiss.BindGridPermissFuncModun(drSelectModun.SelectedValue.ToString());
            if (drSelectModun.SelectedValue.ToString() == "-1")
            {
                if (dtFunctionPermiss != null && dtFunctionPermiss.Rows.Count > 0)
                {
                    chkChucNangList.DataSource = dtFunctionPermiss;
                    chkChucNangList.DataTextField = "DescriptstionName";
                    chkChucNangList.DataValueField = "PermissFuncID";
                    chkChucNangList.DataBind();
                }
            }
            else
            {
                if (dtFunctionPermissModun != null && dtFunctionPermissModun.Rows.Count > 0)
                {
                    chkChucNangList.DataSource = dtFunctionPermissModun;
                    chkChucNangList.DataTextField = "DescriptstionName";
                    chkChucNangList.DataValueField = "PermissFuncID";
                    chkChucNangList.DataBind();
                }
            }
        }
        protected void BindGridUserPermisstionDetail_Edit(int id)
        {
            dtUserPermisstionDetail_Edit = clsUserPermiss.BindGridUserPermissDetail_Edit(id);
            if (dtUserPermisstionDetail_Edit != null && dtUserPermisstionDetail_Edit.Rows.Count > 0)
            {
                lbID.Text = dtUserPermisstionDetail_Edit.Rows[0]["UserPerID"].ToString();
                lbIDXoa.Text = dtUserPermisstionDetail_Edit.Rows[0]["UserPerID"].ToString();
                drRolesGroup.SelectedValue = dtUserPermisstionDetail_Edit.Rows[0]["RolesID"].ToString();
                txtMoDunHienTai.Text = dtUserPermisstionDetail_Edit.Rows[0]["DescriptstionName"].ToString();
                if ((bool)dtUserPermisstionDetail_Edit.Rows[0]["AddNew"] == true)
                {
                    chkThem_Edit.Checked = true;
                }
                else
                {
                    chkThem_Edit.Checked = false;
                }
                //--------------
                if ((bool)dtUserPermisstionDetail_Edit.Rows[0]["Edit"] == true)
                {
                    chkEdit_Edit.Checked = true;
                }
                else
                {
                    chkEdit_Edit.Checked = false;
                }
                //--------------
                if ((bool)dtUserPermisstionDetail_Edit.Rows[0]["Delete"] == true)
                {
                    chkXoa_Edit.Checked = true;
                }
                else
                {
                    chkXoa_Edit.Checked = false;
                }
                //--------------
                if ((bool)dtUserPermisstionDetail_Edit.Rows[0]["Review"] == true)
                {
                    chkDuyet_Edit.Checked = true;
                }
                else
                {
                    chkDuyet_Edit.Checked = false;
                }
                if ((bool)dtUserPermisstionDetail_Edit.Rows[0]["Is_Page"] == true)
                {
                    chkXem_Edit.Checked = true;
                }
                else
                {
                    chkXem_Edit.Checked = false;
                }
            }
        }
        protected void BindGridUserPermisstionDel(int id)
        {
            dtDelUserPermiss = clsUserPermiss.BindGridUserPermissDel(id);
            if (dtDelUserPermiss != null && dtDelUserPermiss.Rows.Count > 0)
            {
                lbIDXoa.Text = dtDelUserPermiss.Rows[0]["UserPerID"].ToString();
            }
        }
        protected void BindGridPermisstionModun(string id)
        {
            dtFunctionPermissModun = clsFunctionPermiss.BindGridPermissFuncModun(id);
            if (dtFunctionPermissModun != null && dtFunctionPermissModun.Rows.Count > 0)
            {
                chkChucNangList.DataSource = dtFunctionPermissModun;
                chkChucNangList.DataTextField = "DescriptstionName";
                chkChucNangList.DataValueField = "PermissFuncID";
                chkChucNangList.DataBind();
            }
        }
        protected void FillterRoles(int id)
        {
            dtFillterRoles = clsUserPermiss.BindGridUserPermissFillter(id);
            if (dtFillterRoles != null && dtFillterRoles.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtFillterRoles;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void FillterModun(int id)
        {
            dtFillterModun = clsUserPermiss.FillterModun(id);
            if (dtFillterModun != null && dtFillterModun.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtFillterModun;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void Insert()
        {
            //Insert
            if (flag)
            {
                InsertRoles();
            }
            //Update
            else
            {
                UpdateRoles();
            }
        }
        protected bool CheckInsert()
        {
            dtCheckInsert = clsUserPermiss.BindGridUserPermiss();
            if (dtCheckInsert != null && dtCheckInsert.Rows.Count > 0)
            {
                foreach (DataRow drCheckInsert in dtCheckInsert.Rows)
                {
                    if (drRolesGroup.SelectedValue.ToString() == drCheckInsert["RolesID"].ToString() && chkChucNangList.SelectedValue.ToString() == drCheckInsert["PermissFuncID"].ToString())
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        protected void InsertRoles()
        {
            try
            {
                VanTaiProperty.clsTMUserPermissProperty obj = new clsTMUserPermissProperty();
                int str = 0;
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Tài khoản này đã được cấp quyền này rồi !";
                    OpenDialog("#thongbao");
                    updatePanelThongBao.Update();
                }
                else
                {
                    #region
                    for (int i = 0; i <= chkChucNangList.Items.Count - 1; i++)
                    {
                        if (chkChucNangList.Items[i].Selected)
                        {
                            str = int.Parse(chkChucNangList.Items[i].Value);
                            obj.RolesID = int.Parse(drRolesGroup.SelectedValue.ToString());
                            obj.PermissFuncID = str;
                            if (chkXem_Edit.Checked)
                            {
                                obj.Is_Page = true;
                            }
                            if (chkThem_Edit.Checked)
                            {
                                obj.PermisstionNumber = 1;
                                obj.AddNew = true;
                            }
                            //-----------------------
                            if (chkEdit_Edit.Checked)
                            {
                                obj.PermisstionNumber = 2;
                                obj.Edit = true;
                            }
                            //-----------------------
                            if (chkXoa_Edit.Checked)
                            {
                                obj.PermisstionNumber = 4;
                                obj.Delete = true;
                            }
                            //-----------------------
                            if (chkDuyet_Edit.Checked)
                            {
                                obj.PermisstionNumber = 8;
                                obj.Review = true;
                            }
                            //----------------------
                            if (chkEdit_Edit.Checked && chkThem_Edit.Checked)
                            {
                                obj.PermisstionNumber = 3;
                                obj.AddNew = true;
                                obj.Edit = true;
                            }
                            //------------------------
                            if (chkXoa_Edit.Checked && chkThem_Edit.Checked)
                            {
                                obj.PermisstionNumber = 5;
                                obj.AddNew = true;
                                obj.Delete = true;
                            }
                            //----------------------------------
                            if (chkDuyet_Edit.Checked && chkThem_Edit.Checked)
                            {
                                obj.PermisstionNumber = 9;
                                obj.AddNew = true;
                                obj.Review = true;
                            }
                            //----------------------------------
                            if (chkEdit_Edit.Checked && chkXoa_Edit.Checked)
                            {
                                obj.PermisstionNumber = 6;
                                obj.Edit = true;
                                obj.Delete = true;
                            }
                            //----------------------------------
                            if (chkEdit_Edit.Checked && chkDuyet_Edit.Checked)
                            {
                                obj.PermisstionNumber = 10;
                                obj.Edit = true;
                                obj.Review = true;
                            }
                            //----------------------------------
                            if (chkXoa_Edit.Checked && chkDuyet_Edit.Checked)
                            {
                                obj.PermisstionNumber = 12;
                                obj.Delete = true;
                                obj.Review = true;
                            }
                            //----------------------------------
                            if (chkThem_Edit.Checked && chkEdit_Edit.Checked && chkXoa_Edit.Checked && chkDuyet_Edit.Checked)
                            {
                                obj.PermisstionNumber = 15;
                                obj.AddNew = true;
                                obj.Edit = true;
                                obj.Delete = true;
                            }
                            VanTaiBLL.clsBusiness_tb_UserPermiss.InsertRoles(obj);
                        }
                    }
                    #endregion
                    lbThongBaoLoi.Text = "Bạn đã cấp quyền thành công !";
                }
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void UpdateRoles()
        {
            try
            {
                VanTaiProperty.clsTMUserPermissProperty obj = new clsTMUserPermissProperty();
                obj.UserPerID = int.Parse(lbID.Text);
                if (chkXem_Edit.Checked)
                {
                    obj.Is_Page = true;
                }
                if (chkThem_Edit.Checked)
                {
                    obj.PermisstionNumber = 1;
                    obj.AddNew = true;
                }
                //-----------------------
                if (chkEdit_Edit.Checked)
                {
                    obj.PermisstionNumber = 2;
                    obj.Edit = true;
                }
                //-----------------------
                if (chkXoa_Edit.Checked)
                {
                    obj.PermisstionNumber = 4;
                    obj.Delete = true;
                }
                //-----------------------
                if (chkDuyet_Edit.Checked)
                {
                    obj.PermisstionNumber = 8;
                    obj.Delete = true;
                }
                //----------------------
                if (chkEdit_Edit.Checked && chkThem_Edit.Checked)
                {
                    obj.PermisstionNumber = 3;
                    obj.AddNew = true;
                    obj.Edit = true;
                }
                //----------------------------------
                if (chkDuyet_Edit.Checked && chkThem_Edit.Checked)
                {
                    obj.PermisstionNumber = 9;
                    obj.AddNew = true;
                    obj.Review = true;
                }
                //----------------------------------
                if (chkEdit_Edit.Checked && chkDuyet_Edit.Checked)
                {
                    obj.PermisstionNumber = 10;
                    obj.Edit = true;
                    obj.Review = true;
                }
                //----------------------------------
                if (chkXoa_Edit.Checked && chkDuyet_Edit.Checked)
                {
                    obj.PermisstionNumber = 12;
                    obj.Delete = true;
                    obj.Review = true;
                }
                //------------------------
                if (chkXoa_Edit.Checked && chkThem_Edit.Checked)
                {
                    obj.PermisstionNumber = 5;
                    obj.AddNew = true;
                    obj.Delete = true;
                }
                //----------------------------------
                if (chkEdit_Edit.Checked && chkXoa_Edit.Checked)
                {
                    obj.PermisstionNumber = 6;
                    obj.Edit = true;
                    obj.Delete = true;
                }
                //----------------------------------
                if (chkThem_Edit.Checked && chkEdit_Edit.Checked && chkXoa_Edit.Checked && chkDuyet_Edit.Checked)
                {
                    obj.PermisstionNumber = 15;
                    obj.AddNew = true;
                    obj.Edit = true;
                    obj.Delete = true;
                }
                VanTaiBLL.clsBusiness_tb_UserPermiss.UpdateRoles(obj);
                lbThongBaoLoi.Text = "Bạn đã cấp quyền lại thành công !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
                BindGridUserPermisstionDetail_Edit(int.Parse(lbID.Text));
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        //AutoComplate
        protected void Autocomplate()
        {
            dtAutoComplete = clsUserPermiss.BindGridUserPermiss();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["DescriptstionName"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["DescriptstionName"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsUserPermiss.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[ClearCheckBox]
        protected void ClearCheckBox()
        {
            chkThem_Edit.Checked = false;
            chkEdit_Edit.Checked = false;
            chkXoa_Edit.Checked = false;
            chkXem_Edit.Checked = false;
            chkChucNangList.SelectedIndex = -1;
            drSelectModun.SelectedIndex = -1;
            drRolesGroup.SelectedIndex = -1;
        }
        #endregion

        #region[Permisstion]
        protected void CheckPermisstion()
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                bool Is_Page_All = SecurityServices.HasPermisstionPage(StringContent.QuanLyPhanQuyen, int.Parse(cookie["RolesID"]), true);
                if (Is_Page_All == true)
                {
                    bool AddNew = SecurityServices.HasPermisstion(StringContent.QuanLyPhanQuyen, int.Parse(cookie["RolesID"]), TypeaAudit.AddNew);
                    if (AddNew == false)
                    {
                        cmdAddNew.Visible = false;
                    }
                    else
                    {
                        cmdAddNew.Visible = true;
                    }
                    bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyPhanQuyen, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                    if (Edit == false)
                    {
                        cmdEdit.Visible = false;
                    }
                    else
                    {
                        cmdEdit.Visible = true;
                    }
                    bool Del = SecurityServices.HasPermisstion(StringContent.QuanLyPhanQuyen, int.Parse(cookie["RolesID"]), TypeaAudit.Delete);
                    if (Del == false)
                    {
                        cmdDel.Visible = false;
                    }
                    else
                    {
                        cmdDel.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("error-page.aspx");
                }
            }
        }
        #endregion
    }
}